package com.linchong.concurrency.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author linchong
 * @version 1.0
 * @Date: 2020-12-08 15:58
 * @Description: SynchronizedExample2$
 */
@Slf4j
public class SynchronizedExample2 {

	/**
	 * 修饰一个类
	 */
	public static void test1(int j){
		synchronized (SynchronizedExample2.class){
			for (int i = 0; i < 100; i++) {
				log.info("test1 - {} - {}",j,i);
			}
		}
	}

	/**
	 * 修饰一个静态方法
	 *
	 */
	public static synchronized void test2(int j){
		for (int i = 0; i < 100; i++) {
			log.info("test2 {} - {}",j,i);
		}
	}

	public static void main(String[] args) {
		SynchronizedExample2 instance = new SynchronizedExample2();
		SynchronizedExample2 instance2 = new SynchronizedExample2();
		ExecutorService executorService = Executors.newCachedThreadPool();


		executorService.execute(()->instance.test1(1));
		executorService.execute(()->instance2.test1(2));

	}
}








